/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.bionic.socialNet.dao.implementations;

import com.bionic.socialNet.dao.interfaces.IUserDAO;
import com.bionic.socialNet.entities.LoginData;
import com.bionic.socialNet.entities.UserData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Ann
 */
public class OraUser extends QueryExecutor implements IUserDAO {

    private final String SELECT_USER = "select * from users where login_email = '%s'";
    private final String INSERT_USER = "insert into users(user_id, login_email, is_active, user_type, password_hash)"
            + " values  (SEQ_USER.NEXTVAL, '%s', 1, 0, '%s')"; // login,passHash
    private final String SET_ENABLED = "update users set is_active = %d  where user_id = v_user_id";
    private final String FIND_IS_ADMIN = "select * from users where login_email = '%s' and user_type = %d";

    @Override
    public boolean setUserEnabled(int UserID, boolean IsEnabled) {
        int enabled = (IsEnabled) ? 1 : 0;
        try {
            query.executeUpdateStatement(String.format(SET_ENABLED, UserID, enabled));
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    // TEST
    @Override
    public UserData findUser(String userLogin) {
        try {
            ResultSet RS = query.getRSForQuery(String.format(SELECT_USER, userLogin));
            UserData user = new UserData();
            while (RS.next()) {
                user.setUserID(RS.getInt("USER_ID"));
                user.setUserLoginName(RS.getString("LOGIN_EMAIL"));
            }
            return user;
        } catch (SQLException ex) {
            Logger.getLogger(OraUser.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }

    // used for reset password
    @Override
    public boolean updateUser(UserData userData) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean isAdmin(int userID) {
        int i = 0;
        try {
            ResultSet rs = query.getRSForQuery(String.format(FIND_IS_ADMIN, userID, 100));
            i = query.getRSRecordCount(rs);
        } catch (Exception e) {
            return false;
        }
        return i > 0;
    }

    @Override
    public boolean registerUser(LoginData loginData) {
        String queryText;
        queryText = String.format(INSERT_USER, loginData.getLogin(), loginData.getPassworgHash());
        boolean insertresult = query.executeInsertStatement(queryText);
        return insertresult;
    }

    @Override
    public ArrayList<UserData> selectUsersList(String Criteria) {
        String sqlText = "select u.*, 1 as is_admin from users u where u.user_type = 100"
                + " union select u.*, 0 as is_admin from users u where u.user_type <> 100";
        ResultSet RS = query.getRSForQuery(sqlText);
        ArrayList<UserData> userDataList = new ArrayList<UserData>();
        try {
            while (RS.next()) {
                UserData user = new UserData();
                user.setIsActive(RS.getInt("is_active") == 1);
                user.setIsAdmin(RS.getInt("is_admin") == 1);
                user.setUserID(RS.getInt("user_id"));
                user.setUserLoginName(RS.getString("LOGIN_EMAIL"));
                userDataList.add(user);
            }
        } catch (SQLException ex) {
            Logger.getLogger(OraUser.class.getName()).log(Level.SEVERE, null,
                    ex);
        }
        return userDataList;
    }

    @Override
    public boolean isLoginAlreadyTaken(String login) {
        // TODO Auto-generated method stub
        ResultSet rs = query.getRSForQuery(String.format(SELECT_USER, login));
        int count = 0;
        try {
            count = query.getRSRecordCount(rs);
        } catch (SQLException e) {
            return false;
        }
        return (count > 0);
    }

    @Override
    public boolean isLoginExecute(LoginData loginData) {
        String sqlText = "select * from users u where "
                + "u.login_email = '%s' and u.password_hash = '%s' "
                + " and u.is_active = 1";
         ResultSet rs = query.getRSForQuery(String.format(SELECT_USER, loginData.getLogin(),loginData.getPassworgHash()));
        int count = 0;
        try {
            count = query.getRSRecordCount(rs);
        } catch (SQLException e) {
            return false;
        }
        return (count > 0);
    }
}
